<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-09
 * Time: 14:07
 */

namespace app\api\controller;

use app\common\model\ChatCustomerMsg;
use app\service\CustomerService;

/**
 * 客服
 * Class Customermsg
 * @package app\api\controller
 */
class Customermsg extends Base
{
    public $chatCustomerMsgModel;

    public function __construct()
    {
        parent::__construct();
        $this->chatCustomerMsgModel = new ChatCustomerMsg();
    }


    /**
     * 消息列表
     */
    public function msg_list()
    {
        if (request()->isPost()) {
            $data = input('');
            $user_id = $data['kid'];
            $id = $data['last_id']?$data['last_id']:$this->chatCustomerMsgModel->max('id')+1;
            $token = $data['token'];
            if ($token != md5($id . 'zkhb')) {
                //$this->ajax(202,'身份校验失败');
            }
            $page = max(input('page'), 1);
            $page_num = 20;
            $where = '';
            $where .= "`revoke`=0 and status=1 and mall_id={$this->mall_id} and id < {$id} and ((uid={$this->uid} and kid={$user_id}) or (uid={$user_id} and kid={$this->uid}))";
            $result = CustomerService::S()->queryPage($where, $page, $page_num);
            if (!$result) {
                $this->ajax(200, 'success', []);
            }
            $last_id = $result[0]['id'];
            foreach ($result as $k => $v) {
                $result[$k]['content'] = json_decode($v['content'], true);
                $last_id = $v['id'];
                $result[$k]['create_time'] = $v['create_time'];
            }
            $backData['list'] = $result;
            $backData['last_id'] = $last_id;
            $this->ajax(200, 'success', $backData);
        }
    }


    /**
     * 客服消息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function customer_list()
    {
        if (request()->isPost()) {
            $data = input('');
            $page = max(1, $data['page']);
            $size = 20;
            $list = $this->chatCustomerMsgModel
                ->where('kid|uid', $this->uid)
                ->order('max_id desc')
                ->group('group_id')
                ->field('uid,kid,content,create_time,group_id,max(id) as max_id')
                ->page($page, $size)
                ->select()
                ->toArray();
            if($list){
                foreach ($list as $key => $value){
                    $user=\app\common\model\User::getOne(['id'=>$value['uid']!=$this->uid?$value['uid']:$value['kid']],'nickname,headimgurl');
                    $chat = $this->chatCustomerMsgModel->where('id',$value['max_id'])->field('content,create_time')->find()->toArray();
                    $value['content'] =$chat['content'];
                    $value['create_time'] = $chat['create_time'];
                    $value['content'] = json_decode($value['content'],true);
                    $list[$key] = array_merge($value,$user);

                }
            }
            $this->ajax(200, 'success', $list);
        }
    }


}

















